//
// SoapTrace.Net - A soap message monitoring utility. 
//
// Copyright (C) 2005 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using NUnit.Framework;
using SoapTrace.Core.Communication;
using SoapTrace.Core.Diagnostics;
using System;
using System.IO;
using System.Text;
using WinPcap;

namespace SoapTrace.Tests.SoapPacketMonitoring
{
	/// <summary>
	/// Tests that the <see cref="SoapPacketMonitorBase"/> class's
	/// handling of a soap request and response.
	/// </summary>
	[ TestFixture ]
	public class IPPacketRequestTestFixture : SoapPacketMonitorBase
	{
		SoapRequest Request;
		SoapResponse Response;
		
		/// <summary>
		/// Initialises the test fixture.
		/// </summary>
		[ TestFixtureSetUp ]
		public void Init( )
		{
			Request = null;
			Response = null;
			
			base.ClearClients( );
			
			base.SetFilter( "192.168.1.3", 80 );
			
			StringReader Reader = new StringReader( GetPacketXml( ) );
			PacketLogEntryCollection LogEntries = PacketLogFile.Load( Reader );
			
			foreach( PacketLogEntry Entry in LogEntries )
			{
				IPPacket IPPacket = new IPPacket( Entry.Packet.GetBytes( ) );
				base.ProcessIPPacket( IPPacket );
			}
		}
			
		/// <summary>
		/// Check we received the correct message.
		/// </summary>
		[ Test ]
		public void SoapRequestMessage( )
		{
			string ExpectedMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
				"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n" +
				"  <soap:Body>\r\n" +
				"    <Test xmlns=\"http://tempuri.org/\">\r\n" +
				"      <xml>Re</xml>\r\n" +
				"        <text>Test text here...</text>\r\n" +
				"    </Test>\r\n" +
				"  </soap:Body>\r\n" +
				"</soap:Envelope>";
			
			Assert.AreEqual( ExpectedMessage, Request.Message, "Unexpected message received." );
		}
		
		/// <summary>
		/// Check we received the correct message.
		/// </summary>
		[ Test ]
		public void SoapResponseMessage( )
		{
			string ExpectedMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
				"<soap:Body><TestResponse xmlns=\"http://tempuri.org/\">" +
				"<TestResult><string>&lt;ticket&gt;&lt;user&gt;matt&lt;/user&gt;&lt;password&gt;secret&lt;/password&gt;&lt;/ticket&gt;</string>" +
				"<string>C2JIZQAA</string>" +
				"</TestResult></TestResponse></soap:Body></soap:Envelope>";
			
			Assert.AreEqual( ExpectedMessage, Response.Message, "Unexpected message received." );
		}
	
		/// <summary>
		/// Handles the soap request received event.
		/// </summary>
		protected override void OnSoapRequestReceived( object sender, SoapRequestReceivedEventArgs e )
		{
			Request = e.Request;
		}
		
		/// <summary>
		/// Handles the soap response received event.
		/// </summary>
		protected override void OnSoapResponseReceived( object sender, SoapResponseReceivedEventArgs e )
		{
			Response = e.Response;
		}		
		
		/// <summary>
		/// Gets the xml to be read in by the <see cref="PacketLogFile"/>
		/// class.
		/// </summary>
		private string GetPacketXml( )
		{
			return "<Packets>\r\n" +
				"  <Packet Time=\"2005-02-09 15:23:49.31\" ServerFilter=\"192.168.1.3\" PortFilter=\"80\" PacketType=\"IP\">\r\n" +
				"    <Bytes>45 00 00 30 0F C9 40 00 80 06 67 A7 C0 A8 01 04 C0 A8 01 03 05 1F 00 50 3B 3E A5 43 00 00 00 00 70 02 FF FF 19 D7 00 00 02 04 05 B4 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-09 15:23:49.32\" ServerFilter=\"192.168.1.3\" PortFilter=\"80\" PacketType=\"IP\">\r\n" +
				"    <Bytes>45 00 00 28 0F CA 40 00 80 06 67 AE C0 A8 01 04 C0 A8 01 03 05 1F 00 50 3B 3E A5 44 57 3E 79 6A 50 10 FF FF 75 E2 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-09 15:23:49.32\" ServerFilter=\"192.168.1.3\" PortFilter=\"80\" PacketType=\"IP\">\r\n" +
				"    <Bytes>45 00 00 30 08 71 40 00 7F 06 6F FF C0 A8 01 03 C0 A8 01 04 00 50 05 1F 57 3E 79 69 3B 3E A5 44 70 12 FF FF 49 46 00 00 02 04 05 8C 01 01 04 02 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-09 15:23:49.32\" ServerFilter=\"192.168.1.3\" PortFilter=\"80\" PacketType=\"IP\">\r\n" +
				"    <Bytes>45 00 00 F8 0F CB 40 00 80 06 66 DD C0 A8 01 04 C0 A8 01 03 05 1F 00 50 3B 3E A5 44 57 3E 79 6A 50 18 FF FF 45 DA 00 00 50 4F 53 54 20 2F 4D 61 74 68 57 65 62 53 65 72 76 69 63 65 2F 4D 61 74 68 73 2E 61 73 6D 78 20 48 54 54 50 2F 31 2E 31 0D 0A 53 6F 61 70 41 63 74 69 6F 6E 3A 20 22 68 74 74 70 3A 2F 2F 74 65 6D 70 75 72 69 2E 6F 72 67 2F 54 65 73 74 22 0D 0A 43 6F 6E 74 65 6E 74 2D 54 79 70 65 3A 20 74 65 78 74 2F 78 6D 6C 3B 20 63 68 61 72 73 65 74 3D 75 74 66 2D 38 0D 0A 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20 33 37 32 0D 0A 45 78 70 65 63 74 3A 20 31 30 30 2D 63 6F 6E 74 69 6E 75 65 0D 0A 43 6F 6E 6E 65 63 74 69 6F 6E 3A 20 4B 65 65 70 2D 41 6C 69 76 65 0D 0A 48 6F 73 74 3A 20 31 39 32 2E 31 36 38 2E 31 2E 33 0D 0A 0D 0A </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-09 15:23:49.32\" ServerFilter=\"192.168.1.3\" PortFilter=\"80\" PacketType=\"IP\">\r\n" +
				"    <Bytes>45 00 00 98 08 72 40 00 7F 06 6F 96 C0 A8 01 03 C0 A8 01 04 00 50 05 1F 57 3E 79 6A 3B 3E A6 14 50 18 FF 2F 89 34 00 00 48 54 54 50 2F 31 2E 31 20 31 30 30 20 43 6F 6E 74 69 6E 75 65 0D 0A 53 65 72 76 65 72 3A 20 4D 69 63 72 6F 73 6F 66 74 2D 49 49 53 2F 35 2E 30 0D 0A 44 61 74 65 3A 20 57 65 64 2C 20 30 39 20 46 65 62 20 32 30 30 35 20 31 35 3A 32 33 3A 32 31 20 47 4D 54 0D 0A 58 2D 50 6F 77 65 72 65 64 2D 42 79 3A 20 41 53 50 2E 4E 45 54 0D 0A 0D 0A </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-09 15:23:49.32\" ServerFilter=\"192.168.1.3\" PortFilter=\"80\" PacketType=\"IP\">\r\n" +
				"    <Bytes>45 00 01 9C 0F CC 40 00 80 06 66 38 C0 A8 01 04 C0 A8 01 03 05 1F 00 50 3B 3E A6 14 57 3E 79 DA 50 18 FF 8F 2A 32 00 00 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D 22 75 74 66 2D 38 22 3F 3E 0D 0A 3C 73 6F 61 70 3A 45 6E 76 65 6C 6F 70 65 20 78 6D 6C 6E 73 3A 78 73 69 3D 22 68 74 74 70 3A 2F 2F 77 77 77 2E 77 33 2E 6F 72 67 2F 32 30 30 31 2F 58 4D 4C 53 63 68 65 6D 61 2D 69 6E 73 74 61 6E 63 65 22 20 78 6D 6C 6E 73 3A 78 73 64 3D 22 68 74 74 70 3A 2F 2F 77 77 77 2E 77 33 2E 6F 72 67 2F 32 30 30 31 2F 58 4D 4C 53 63 68 65 6D 61 22 20 78 6D 6C 6E 73 3A 73 6F 61 70 3D 22 68 74 74 70 3A 2F 2F 73 63 68 65 6D 61 73 2E 78 6D 6C 73 6F 61 70 2E 6F 72 67 2F 73 6F 61 70 2F 65 6E 76 65 6C 6F 70 65 2F 22 3E 0D 0A 20 20 3C 73 6F 61 70 3A 42 6F 64 79 3E 0D 0A 20 20 20 20 3C 54 65 73 74 20 78 6D 6C 6E 73 3D 22 68 74 74 70 3A 2F 2F 74 65 6D 70 75 72 69 2E 6F 72 67 2F 22 3E 0D 0A 20 20 20 20 20 20 3C 78 6D 6C 3E 52 65 3C 2F 78 6D 6C 3E 0D 0A 20 20 20 20 20 20 20 20 3C 74 65 78 74 3E 54 65 73 74 20 74 65 78 74 20 68 65 72 65 2E 2E 2E 3C 2F 74 65 78 74 3E 0D 0A 20 20 20 20 3C 2F 54 65 73 74 3E 0D 0A 20 20 3C 2F 73 6F 61 70 3A 42 6F 64 79 3E 0D 0A 3C 2F 73 6F 61 70 3A 45 6E 76 65 6C 6F 70 65 3E </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-09 15:23:49.33\" ServerFilter=\"192.168.1.3\" PortFilter=\"80\" PacketType=\"IP\">\r\n" +
				"    <Bytes>45 00 02 E0 08 73 40 00 7F 06 6D 4D C0 A8 01 03 C0 A8 01 04 00 50 05 1F 57 3E 79 DA 3B 3E A7 88 50 18 FD BB FB 14 00 00 48 54 54 50 2F 31 2E 31 20 32 30 30 20 4F 4B 0D 0A 53 65 72 76 65 72 3A 20 4D 69 63 72 6F 73 6F 66 74 2D 49 49 53 2F 35 2E 30 0D 0A 44 61 74 65 3A 20 57 65 64 2C 20 30 39 20 46 65 62 20 32 30 30 35 20 31 35 3A 32 33 3A 32 31 20 47 4D 54 0D 0A 58 2D 50 6F 77 65 72 65 64 2D 42 79 3A 20 41 53 50 2E 4E 45 54 0D 0A 58 2D 41 73 70 4E 65 74 2D 56 65 72 73 69 6F 6E 3A 20 31 2E 31 2E 34 33 32 32 0D 0A 43 61 63 68 65 2D 43 6F 6E 74 72 6F 6C 3A 20 70 72 69 76 61 74 65 2C 20 6D 61 78 2D 61 67 65 3D 30 0D 0A 43 6F 6E 74 65 6E 74 2D 54 79 70 65 3A 20 74 65 78 74 2F 78 6D 6C 3B 20 63 68 61 72 73 65 74 3D 75 74 66 2D 38 0D 0A 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20 34 36 37 0D 0A 0D 0A 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D 22 75 74 66 2D 38 22 3F 3E 3C 73 6F 61 70 3A 45 6E 76 65 6C 6F 70 65 20 78 6D 6C 6E 73 3A 73 6F 61 70 3D 22 68 74 74 70 3A 2F 2F 73 63 68 65 6D 61 73 2E 78 6D 6C 73 6F 61 70 2E 6F 72 67 2F 73 6F 61 70 2F 65 6E 76 65 6C 6F 70 65 2F 22 20 78 6D 6C 6E 73 3A 78 73 69 3D 22 68 74 74 70 3A 2F 2F 77 77 77 2E 77 33 2E 6F 72 67 2F 32 30 30 31 2F 58 4D 4C 53 63 68 " +
				"65 6D 61 2D 69 6E 73 74 61 6E 63 65 22 20 78 6D 6C 6E 73 3A 78 73 64 3D 22 68 74 74 70 3A 2F 2F 77 77 77 2E 77 33 2E 6F 72 67 2F 32 30 30 31 2F 58 4D 4C 53 63 68 65 6D 61 22 3E 3C 73 6F 61 70 3A 42 6F 64 79 3E 3C 54 65 73 74 52 65 73 70 6F 6E 73 65 20 78 6D 6C 6E 73 3D 22 68 74 74 70 3A 2F 2F 74 65 6D 70 75 72 69 2E 6F 72 67 2F 22 3E 3C 54 65 73 74 52 65 73 75 6C 74 3E 3C 73 74 72 69 6E 67 3E 26 6C 74 3B 74 69 63 6B 65 74 26 67 74 3B 26 6C 74 3B 75 73 65 72 26 67 74 3B 6D 61 74 74 26 6C 74 3B 2F 75 73 65 72 26 67 74 3B 26 6C 74 3B 70 61 73 73 77 6F 72 64 26 67 74 3B 73 65 63 72 65 74 26 6C 74 3B 2F 70 61 73 73 77 6F 72 64 26 67 74 3B 26 6C 74 3B 2F 74 69 63 6B 65 74 26 67 74 3B 3C 2F 73 74 72 69 6E 67 3E 3C 73 74 72 69 6E 67 3E 43 32 4A 49 5A 51 41 41 3C 2F 73 74 72 69 6E 67 3E 3C 2F 54 65 73 74 52 65 73 75 6C 74 3E 3C 2F 54 65 73 74 52 65 73 70 6F 6E 73 65 3E 3C 2F 73 6F 61 70 3A 42 6F 64 79 3E 3C 2F 73 6F 61 70 3A 45 6E 76 65 6C 6F 70 65 3E </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"  <Packet Time=\"2005-02-09 15:23:49.50\" ServerFilter=\"192.168.1.3\" PortFilter=\"80\" PacketType=\"IP\">\r\n" +
				"    <Bytes>45 00 00 28 0F CD 40 00 80 06 67 AB C0 A8 01 04 C0 A8 01 03 05 1F 00 50 3B 3E A7 88 57 3E 7C 92 50 10 FC D7 73 9E 00 00 </Bytes>\r\n" +
				"  </Packet>\r\n" +
				"</Packets>";
		}
	}
}
